package razor.blade;

import java.util.*;
import java.io.*;
import javafx.beans.property.*;
import javafx.beans.property.DoubleProperty;
import tee.binding.*;

public class UserProperties {
    private String fileName;
    Properties properties;
    Hashtable<String, SimpleDoubleProperty> doubles;
    Hashtable<String, SimpleStringProperty> strings;
    Hashtable<String, Numeric> numerics;
    public UserProperties(String key) {
	fileName = System.getProperty("user.home") + System.getProperty("file.separator") + key;
	properties = new Properties();
	numerics = new Hashtable<String, Numeric>();
	doubles = new Hashtable<String, SimpleDoubleProperty>();
	strings = new Hashtable<String, SimpleStringProperty>();
	load();
	Runtime.getRuntime().addShutdownHook(new Thread() {
	    public void run() {
		save();
	    }
	});
    }
    public Numeric asNumeric(String name, double defaultValue) {
	Numeric p = numerics.get(name);
	if (p == null) {
	    p = new Numeric().value(defaultValue);
	    try {
		numerics.put(name, p);
		String s = properties.getProperty(name);
		if (s != null) {
		    Double d = Double.parseDouble(s);
		    p.value(d);
		}
	    } catch (Throwable t) {
		t.printStackTrace();
	    }
	}
	double n1 = p.value().doubleValue();
	double n2 = n1;
	n2++;
	n2--;
	if (n1 == n2) {
	    //not a NaN
	} else {
	    //System.out.println("property "+name + ": " + n1 );
	    p.value(defaultValue);
	}
	//System.out.println(name+": "+nn);
	//nn++;
	//System.out.println(name+": "+nn);
	return p;
    }
    /*public SimpleDoubleProperty asDouble(String name, double defaultValue) {
    SimpleDoubleProperty p = doubles.get(name);
    if (p == null) {
    //System.out.println("create property "+name);
    p = new SimpleDoubleProperty(defaultValue);
    try {
    doubles.put(name, p);
    String s = properties.getProperty(name);
    if (s != null) {
    Double d = Double.parseDouble(s);
    p.set(d);
    }
    } catch (Throwable t) {
    t.printStackTrace();
    }
    }
    return p;
    }*/
    /*public SimpleStringProperty asString(String name, String defaultValue) {
    SimpleStringProperty p = strings.get(name);
    if (p == null) {
    p = new SimpleStringProperty(defaultValue);
    try {
    strings.put(name, p);
    String s = properties.getProperty(name);
    if (s != null) {
    p.set(s);
    }
    } catch (Throwable t) {
    t.printStackTrace();
    }
    }
    return p;
    }*/
    private void save() {
	try {
	    /*for (Enumeration<String> e = doubles.keys(); e.hasMoreElements();) {
	    String k = e.nextElement();
	    properties.setProperty(k, "" + doubles.get(k).get());
	    }*/
	    for (Enumeration<String> e = numerics.keys(); e.hasMoreElements();) {
		String k = e.nextElement();
		properties.setProperty(k, "" + numerics.get(k).value());
	    }
	    /*for (Enumeration<String> e = strings.keys(); e.hasMoreElements();) {
	    String k = e.nextElement();
	    properties.setProperty(k, "" + strings.get(k).get());
	    }*/
	    properties.store(new FileOutputStream(fileName), fileName);
	} catch (Throwable t) {
	    t.printStackTrace();
	}
    }
    private void load() {
	try {
	    properties.load(new FileInputStream(fileName));
	} catch (Throwable t) {
	    t.printStackTrace();
	}
    }
    /*public static void main(String[] a) {
	double nan = 0;
	//nan = nan / nan;
	double h = nan;
	h++;
	h--;
	System.out.println("h: " + h + ", nan: " + nan + ", = " + (h == nan));
    }*/
}
